/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminModTrabajador.java
 *
 * Created on 16-sep-2009, 2:31:36
 */

package com.jomijushi.fid.interfaz;

import com.jomijushi.fid.dominio.FidRolTrabajador;
import com.jomijushi.fid.dominio.FidTrabajador;
import com.jomijushi.fid.servicios.PersonaEmpresaImplementsService;
import com.jomijushi.fid.servicios.PersonaEmpresaInterfazService;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author miguel
 */
public class AdminModTrabajador extends javax.swing.JDialog {

    String error;
    List<FidRolTrabajador> rolt=null;    // Lista de roles del trabajador extraidos
    List<FidRolTrabajador> lst=null;
    public boolean modificado=false;
    FidTrabajador trabajador = new FidTrabajador();
    Vector roles= new Vector();
    
    /** Creates new form AdminModTrabajador */
    public AdminModTrabajador(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(this);
        ActivarOrBloquearOpciones(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PanelDatos = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        nombres = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        dni = new javax.swing.JTextField();
        email = new javax.swing.JTextField();
        telefono = new javax.swing.JTextField();
        nacimiento = new com.toedter.calendar.JDateChooser();
        direccion = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        descripcion = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        fechacreacion = new com.toedter.calendar.JDateChooser();
        PanelCuenta = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        usuario = new javax.swing.JTextField();
        clave1 = new javax.swing.JPasswordField();
        clave2 = new javax.swing.JPasswordField();
        PanelRol = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ListaRol = new javax.swing.JList();
        BotonAgregarRol = new javax.swing.JButton();
        BotonEliminarRol = new javax.swing.JButton();
        BotonEliminar = new javax.swing.JButton();
        BotonModificar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        buscar = new javax.swing.JTextField();
        BotonBuscar = new javax.swing.JButton();
        BotonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modificar Datos Trabajador");

        PanelDatos.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        jLabel1.setText("Nombres:");

        nombres.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                nombresKeyTyped(evt);
            }
        });

        jLabel2.setText("DNI:");

        jLabel3.setText("Email:");

        jLabel4.setText("Teléfono:");

        jLabel5.setText("Nacimiento:");

        jLabel6.setText("Domicilio:");

        jLabel7.setText("Descripción:");

        dni.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                dniKeyTyped(evt);
            }
        });

        telefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                telefonoKeyTyped(evt);
            }
        });

        nacimiento.setDateFormatString("dd/MM/yyyy");
        nacimiento.setMaxSelectableDate(new java.util.Date(253370786462000L));
        nacimiento.setMinSelectableDate(new java.util.Date(-62135747938000L));

        direccion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                direccionKeyTyped(evt);
            }
        });

        descripcion.setColumns(20);
        descripcion.setFont(new java.awt.Font("Tahoma", 0, 11));
        descripcion.setLineWrap(true);
        descripcion.setRows(5);
        descripcion.setWrapStyleWord(true);
        jScrollPane1.setViewportView(descripcion);

        jLabel8.setText("Fecha Inicio:");

        fechacreacion.setDateFormatString("dd/MM/yyyy");

        javax.swing.GroupLayout PanelDatosLayout = new javax.swing.GroupLayout(PanelDatos);
        PanelDatos.setLayout(PanelDatosLayout);
        PanelDatosLayout.setHorizontalGroup(
            PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelDatosLayout.createSequentialGroup()
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelDatosLayout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(direccion, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                            .addComponent(nacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dni, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(telefono, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(email, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
                            .addComponent(nombres, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)))
                    .addGroup(PanelDatosLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fechacreacion, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        PanelDatosLayout.setVerticalGroup(
            PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelDatosLayout.createSequentialGroup()
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(dni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(fechacreacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        PanelCuenta.setBorder(javax.swing.BorderFactory.createTitledBorder("Cuenta de Usuario"));

        jLabel9.setText("Usuario:");

        jLabel10.setText("Contraseña:");

        jLabel11.setText("Repita Contraseña:");

        usuario.setEditable(false);

        javax.swing.GroupLayout PanelCuentaLayout = new javax.swing.GroupLayout(PanelCuenta);
        PanelCuenta.setLayout(PanelCuentaLayout);
        PanelCuentaLayout.setHorizontalGroup(
            PanelCuentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelCuentaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelCuentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(PanelCuentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(clave2)
                    .addComponent(clave1)
                    .addComponent(usuario, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        PanelCuentaLayout.setVerticalGroup(
            PanelCuentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelCuentaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelCuentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelCuentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(clave1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelCuentaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(clave2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(5, Short.MAX_VALUE))
        );

        PanelRol.setBorder(javax.swing.BorderFactory.createTitledBorder("Asignar Rol"));

        jScrollPane2.setViewportView(ListaRol);

        BotonAgregarRol.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add.png"))); // NOI18N
        BotonAgregarRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonAgregarRolActionPerformed(evt);
            }
        });

        BotonEliminarRol.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete.png"))); // NOI18N
        BotonEliminarRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonEliminarRolActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PanelRolLayout = new javax.swing.GroupLayout(PanelRol);
        PanelRol.setLayout(PanelRolLayout);
        PanelRolLayout.setHorizontalGroup(
            PanelRolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelRolLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PanelRolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(BotonEliminarRol, 0, 0, Short.MAX_VALUE)
                    .addComponent(BotonAgregarRol, javax.swing.GroupLayout.PREFERRED_SIZE, 40, Short.MAX_VALUE)))
        );
        PanelRolLayout.setVerticalGroup(
            PanelRolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelRolLayout.createSequentialGroup()
                .addGroup(PanelRolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelRolLayout.createSequentialGroup()
                        .addComponent(BotonAgregarRol)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotonEliminarRol))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        BotonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete.png"))); // NOI18N
        BotonEliminar.setText("Eliminar");
        BotonEliminar.setEnabled(false);
        BotonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonEliminarActionPerformed(evt);
            }
        });

        BotonModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/disk.png"))); // NOI18N
        BotonModificar.setText("Modificar");
        BotonModificar.setEnabled(false);
        BotonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonModificarActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar Trabajador"));

        jLabel12.setText("DNI:");

        buscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                buscarKeyTyped(evt);
            }
        });

        BotonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/find.png"))); // NOI18N
        BotonBuscar.setText("Buscar");
        BotonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
                .addComponent(BotonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BotonBuscar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        BotonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/cancel.png"))); // NOI18N
        BotonCancelar.setText("Cancelar");
        BotonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(PanelDatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(PanelCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(PanelRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BotonModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotonEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PanelDatos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(PanelRol, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(PanelCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonModificar)
                    .addComponent(BotonEliminar)
                    .addComponent(BotonCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean validado()
    {
        error="";
        if(nombres.getText().length()==0)
            error+="- Ingrese Nombres\n";
        if(dni.getText().length()<8)
            error+="- Ingrese DNI (8 digitos)\n";
        if(usuario.getText().length()==0)
            error+="- Ingrese Usuario\n";
        if(roles.size()==0)
            error+="- Asigne un Rol al Trabajador\n";
        String a=String.valueOf(clave1.getPassword());
        String b=String.valueOf(clave2.getPassword());
        if(a.length()>0 || b.length()>0)
        {
            if(clave1.getPassword().length<6)
                error+="- Ingrese Contraseña (minimo 6 caracteres)\n";
            else if(clave2.getPassword().length<6)
                error+="- Ingrese Nuevamente Contraseña (minimo 6 caracteres)\n";
            else if(a.compareTo(b)!= 0)
                error+="- Las contraseñas no coinciden\n";
        }
        if(error.length()==0)
            return true;
        return false;
    }

    public void Restablecer()
    {
        trabajador = new FidTrabajador();
        modificado=false;
        rolt=null;
        lst=null;
        roles.removeAllElements();
        
        nombres.setText("");
        dni.setText("");
        email.setText("");
        telefono.setText("");
        nacimiento.setDate(null);
        direccion.setText("");
        descripcion.setText("");
        fechacreacion.setDate(new Date());
        usuario.setText("");
        clave1.setText("");
        clave2.setText("");        
        CargarListaRol();
    }
    
    public void CargarVector()
    {
        PersonaEmpresaInterfazService workservice = new PersonaEmpresaImplementsService();
        workservice.setEntityManagerFactory(PanelPrincipal.emf);
        
        rolt = workservice.listarRolesTrabajador(trabajador.getId());
        for(int i=0; i < rolt.size();i++)
        {
            System.out.println("Id de Rol: "+rolt.get(i).getNombre());
            roles.add(rolt.get(i));
        }
    }

    
    public void CargarListaRol()
    {
        Object[] objeto = new Object[roles.size()];
        FidRolTrabajador frt;
        for(int i=0; i<roles.size(); i++)
        {
            frt = new FidRolTrabajador();
            frt = (FidRolTrabajador)roles.get(i);
            objeto[i] = frt.getNombre(); //Agregamos solo los nombres de la tabla
        }
        ListaRol.setListData(objeto);
    }

    public void InsertarVerificarRolDuplicado(FidRolTrabajador frt)
    {
        List<FidRolTrabajador> lista = roles;
        boolean encontrado=false;
        // Antes de agregarlo verificamos si existe en la lista de roles
        for(int i=0; i<lista.size(); i++)
        {
            if(lista.get(i).getNombre().compareTo(frt.getNombre())==0)
                encontrado=true;
        }
        if(!encontrado)
            roles.add(frt);
    }
    
    private void nombresKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nombresKeyTyped
        // TODO add your handling code here:
        SoloTexto(evt);
}//GEN-LAST:event_nombresKeyTyped

    private void dniKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dniKeyTyped
        // TODO add your handling code here:
        if(dni.getText().length()<8){
            SoloDigito(evt);
        } else{
            evt.consume();
        }
}//GEN-LAST:event_dniKeyTyped

    private void telefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_telefonoKeyTyped
        // TODO add your handling code here:
        SoloDigito(evt);
}//GEN-LAST:event_telefonoKeyTyped

    //funcion para validar que sea texto
    private void SoloTexto(java.awt.event.KeyEvent evt)
    {
        char c=evt.getKeyChar();
        if(Character.isDigit(c))
            evt.consume();
    }

    private void SoloDigito(java.awt.event.KeyEvent evt)
    {
        char c=evt.getKeyChar();
        if(!Character.isDigit(c))
            evt.consume();
    }
    
    private void direccionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_direccionKeyTyped
        // TODO add your handling code here:
        //SoloTexto(evt);
}//GEN-LAST:event_direccionKeyTyped

    private void BotonAgregarRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonAgregarRolActionPerformed
        // TODO add your handling code here:
        SeleccionRol selectRol = new SeleccionRol(null,true);
        selectRol.setVisible(true);
        if(selectRol.asignado)  // si es true es correcta la asignacion
            InsertarVerificarRolDuplicado(selectRol.getRol());
        CargarListaRol();
}//GEN-LAST:event_BotonAgregarRolActionPerformed

    private void BotonEliminarRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonEliminarRolActionPerformed
        // Sacamos de la lista el elementos que gemos seleccionado
        if(ListaRol.getSelectedValues().length>1) {
            JOptionPane.showMessageDialog(this, "Seleccione un Rol a la vez", "Error", JOptionPane.WARNING_MESSAGE);
        } else if(ListaRol.getSelectedValues().length==0) {
            JOptionPane.showMessageDialog(this, "Seleccione Rol", "Error", JOptionPane.WARNING_MESSAGE);
        } else {
            int eliminar= ListaRol.getSelectedIndex();
            roles.remove(eliminar);
            CargarListaRol();
        }
    }//GEN-LAST:event_BotonEliminarRolActionPerformed

    private void BotonModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonModificarActionPerformed
        // TODO add your handling code here:
        if(validado())// Los datos son correctos y seran almacenados
        {
            lst=roles;
            PersonaEmpresaInterfazService  workservice = new PersonaEmpresaImplementsService();
            trabajador.setNombres(nombres.getText());
            trabajador.setDniRuc(dni.getText());
            trabajador.setEmail(email.getText());
            trabajador.setTelefono(telefono.getText());
            trabajador.setFechaNac(nacimiento.getDate());
            trabajador.setDireccion(direccion.getText());
            trabajador.setDescripcion(descripcion.getText());
            trabajador.setFechaIngreso(fechacreacion.getDate());
            trabajador.setUsuario(usuario.getText());
            if(clave1.getPassword().length>0)
                trabajador.setContrasenia(String.valueOf(clave1.getPassword()));
            trabajador.setUpdatedAt(new Date());

            workservice.setEntityManagerFactory(PanelPrincipal.emf);
            workservice.actualizar(trabajador, lst);

            JOptionPane.showMessageDialog(this, "Trabajor Modificado Correctamente", "Modificar Trabajador", JOptionPane.INFORMATION_MESSAGE);

            modificado=true;
            this.setVisible(false);
        }
        else
            JOptionPane.showMessageDialog(this, error, "Error", JOptionPane.WARNING_MESSAGE);
    }//GEN-LAST:event_BotonModificarActionPerformed


    private void BotonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonEliminarActionPerformed
        PersonaEmpresaInterfazService workservice = new PersonaEmpresaImplementsService();
        workservice.setEntityManagerFactory(PanelPrincipal.emf);
        lst = roles;
        int confirmado = JOptionPane.showConfirmDialog(null,
        "¿Esta Seguro que desea eliminar este registro?");
        if (JOptionPane.OK_OPTION == confirmado){
            workservice.eliminar(trabajador, lst);
            JOptionPane.showMessageDialog(this, "El registro ha sido eliminado.", "Eliminar Registro", JOptionPane.INFORMATION_MESSAGE);
            modificado=true;
            this.setVisible(false);
        }else{
            System.out.println("vale... no borro nada...");
        }
    }//GEN-LAST:event_BotonEliminarActionPerformed

    private void ActivarOrBloquearOpciones(boolean val)
    {
        BotonModificar.setEnabled(val);
        BotonEliminar.setEnabled(val);
        nombres.setEditable(val);
        dni.setEditable(val);
        email.setEditable(val);
        telefono.setEditable(val);
        nacimiento.setEnabled(val);
        direccion.setEditable(val);
        descripcion.setEditable(val);
        fechacreacion.setEnabled(val);
        clave1.setEditable(val);
        clave2.setEditable(val);
        BotonAgregarRol.setEnabled(val);
        BotonEliminarRol.setEnabled(val);
    }

    private void BotonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonBuscarActionPerformed
        // Buscamos al Trabajador
        if(buscar.getText().length()==8){
            PersonaEmpresaInterfazService workservice = new PersonaEmpresaImplementsService();
            workservice.setEntityManagerFactory(PanelPrincipal.emf);
            trabajador = (FidTrabajador)workservice.buscar("fidTrabajador","dniRuc", buscar.getText());

            if(trabajador!=null){
                ActualizarCampos();
                CargarVector();
                CargarListaRol();
                ActivarOrBloquearOpciones(true);
            }
            else
            {
                JOptionPane.showMessageDialog(this, "No se encontrarn resultados", "Error", JOptionPane.WARNING_MESSAGE);
                ActivarOrBloquearOpciones(false);
                Restablecer();
            }
        }else
        {
            JOptionPane.showMessageDialog(this, "Ingrese DNI de 8 difitos", "Error", JOptionPane.WARNING_MESSAGE);
            ActivarOrBloquearOpciones(false);
            Restablecer();
        }
    }//GEN-LAST:event_BotonBuscarActionPerformed

    private void ActualizarCampos()
    {
        nombres.setText(trabajador.getNombres());
        dni.setText(trabajador.getDniRuc());
        email.setText(trabajador.getEmail());
        telefono.setText(trabajador.getTelefono());
        nacimiento.setDate(trabajador.getFechaNac());
        direccion.setText(trabajador.getDireccion());
        descripcion.setText(trabajador.getDescripcion());
        fechacreacion.setDate(trabajador.getFechaIngreso());
        usuario.setText(trabajador.getUsuario());
        roles.removeAllElements();
    }
    private void BotonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCancelarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BotonCancelarActionPerformed

    private void buscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_buscarKeyTyped
        // TODO add your handling code here:
        if(buscar.getText().length()<8){
            SoloDigito(evt);
        }
        else{
            evt.consume();
        }
    }//GEN-LAST:event_buscarKeyTyped

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AdminModTrabajador dialog = new AdminModTrabajador(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonAgregarRol;
    private javax.swing.JButton BotonBuscar;
    private javax.swing.JButton BotonCancelar;
    private javax.swing.JButton BotonEliminar;
    private javax.swing.JButton BotonEliminarRol;
    private javax.swing.JButton BotonModificar;
    private javax.swing.JList ListaRol;
    private javax.swing.JPanel PanelCuenta;
    private javax.swing.JPanel PanelDatos;
    private javax.swing.JPanel PanelRol;
    private javax.swing.JTextField buscar;
    private javax.swing.JPasswordField clave1;
    private javax.swing.JPasswordField clave2;
    private javax.swing.JTextArea descripcion;
    private javax.swing.JTextField direccion;
    private javax.swing.JTextField dni;
    private javax.swing.JTextField email;
    private com.toedter.calendar.JDateChooser fechacreacion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private com.toedter.calendar.JDateChooser nacimiento;
    private javax.swing.JTextField nombres;
    private javax.swing.JTextField telefono;
    private javax.swing.JTextField usuario;
    // End of variables declaration//GEN-END:variables

}
